#!/usr/bin/env python
# -*- coding: utf-8 -*-
import gtk
import gobject

class Tool(object):
    """A tool for the tool box.
    """

    def __init__(self, ActionType, action_params=(),
                 name=None, description=None, icon=None):
        """Make a tool with the given ActionType.

        action_params can be a sequence that will be passed to
        ActionType constructor or a callable that should return a
        sequence of parameters.
        """
        self.ActionType = ActionType
        self.action_params = action_params
        self.description = description or name

        self.name = name
        if isinstance(icon, str):
            self.icon =  gtk.image_new_from_file(icon)
        else:
            self.icon = icon

    def get_action(self):
        """Get the tool's action.
        """
        if callable(self.action_params):
            params = self.action_params()
        else:
            params = self.action_params

        return self.ActionType(*params)


class ToolPallete(gtk.Table):
    """Keeps a grid of radio buttons to represent selectable layer
    editing tools.
    """

    def __init__(self, columns, tools=None):
        """
        """
        super(ToolPallete, self).__init__()
        self.columns = columns
        self.tools = []
        self.group = None

        for tool in tools:
            self.add_tool(tool)

    def add_tool(self, tool):
        """Adds a tool to the end of the tool pallete.
        """
        # Make the first tool added the active tool
        if not self.tools:
            self.active = tool

        self.tools.append(tool)

        btn = self.make_tool_button(tool)

        row, col = divmod(len(self.tools)-1, self.columns)
        self.attach(btn, col, col+1, row, row+1)

    def make_tool_button(self, tool):
        """Create a button for the tool.
        """
        if self.group:
            btn = gtk.RadioButton(self.group)
        else:
            self.group = gtk.RadioButton()
            btn = self.group

        btn.set_mode(False)
        btn.set_tooltip_text(tool.description or tool.name)
        if tool.icon:
            btn.add(tool.icon)
        else:
            btn.set_label(btn.get_tooltip_text())

        btn.connect("toggled", self.tool_callback, tool)

        return btn

    def tool_callback(self, btn, tool):
        """A callback for the tool buttons that change the active
        tool.
        """
        if btn.get_active():
            self.active = tool
            self.emit("tool-changed")

gobject.type_register(ToolPallete)
gobject.signal_new("tool-changed", ToolPallete,
                   gobject.SIGNAL_RUN_LAST,
                   gobject.TYPE_BOOLEAN,
                   ())

class ToolBox(gtk.VBox):
    """A widget that show layer manipulation tools and it's options.
    """

    def __init__(self, tools):
        """
	"""
	super(ToolBox, self).__init__()
        self.pack_start(ToolPallete())



if __name__ == '__main__':
    class A(object):
        pass

    tools = [
        Tool(A, icon="./icons/draw-freehand.png",
             description = "Draw freely with tiles."),
        Tool(A, icon="./icons/draw-eraser.png",
             description = "Draw freely with tiles."),
        Tool(A, icon="./icons/draw-rectangle.png",
             description = "Draw freely with tiles."),
        Tool(A, icon="./icons/select-rectangular.png",
             description = "Draw freely with tiles."),
        Tool(A, icon="./icons/select-rectangular.png",
             description = "Draw freely with tiles."),
        Tool(A, icon="./icons/color-fill.png",
             description = "Draw freely with tiles."),
        Tool(A, icon="./icons/segment-line.png",
             description = "Draw freely with tiles."),
        ]

    def cb(tp):
        print tp.active

    tp = ToolPallete(4, tools)
    tp.connect("tool-changed", cb)


    win = gtk.Window()
    win.add(tp)

    win.show_all()
    win.connect("delete-event", gtk.main_quit)
    gtk.main()
